package com.bd.scm.module.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bd.scm.dao.mapper.user.UserViewMapper;
import com.bd.scm.domain.dto.repairAttendance.BdRepairAddendanceDto;
import com.bd.scm.domain.dto.user.UserDto;
import com.bd.scm.domain.dto.user.UserInfoBaseDto;
import com.bd.scm.domain.models.schedule.ScheduleGroupEmployee;
import com.bd.scm.domain.models.user.UserInfo;
import com.bd.scm.domain.models.user.UserView;
import com.bd.scm.module.user.service.IUserViewService;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserViewService  extends ServiceImpl<UserViewMapper, UserView> implements IUserViewService {

    @Autowired
    private UserViewMapper userViewMapper;

    @Autowired
    private ModelMapper modelMapper;

    public IPage<UserView> getUserList(int pageIndex, int pageSize, UserView entityQuery){
        Page<UserView> page = new Page<>(pageIndex, pageSize);


        QueryWrapper<UserView> query = new QueryWrapper<>(entityQuery);

        return this.userViewMapper.selectPage(page, query);
    }

    /**
     * 根据工号姓名部门模糊查找员工
     * @param query
     * @return
     */
    @Override
    public IPage<UserInfoBaseDto> getListByNoNameDept(UserDto query) {
        Page<UserView> page = new Page<>(query.getCurrent(), query.getPageSize());

        QueryWrapper<UserView> queryWrapper = new QueryWrapper<>();
        //排除离职
        queryWrapper.ne("user_type", 51);

        if (!CollectionUtils.isEmpty(query.getExcludeUserIds())) {
            queryWrapper.notIn("user_serial", query.getExcludeUserIds());
        }
        //and (user_no like '%%' or user_lname like '%%' or dep_name like '%%')
        queryWrapper.and(i -> i.like("user_no", query.getSearchName()).or().like("user_lname", query.getSearchName())
                .or().like("dep_name", query.getSearchName()));

        IPage<UserView> userViewIPage = this.userViewMapper.selectPage(page, queryWrapper);

        IPage<UserInfoBaseDto> listReturn = new Page<>(userViewIPage.getCurrent(), userViewIPage.getSize(), userViewIPage.getTotal());
        if (!CollectionUtils.isEmpty(userViewIPage.getRecords())) {
            List<UserInfoBaseDto> list = modelMapper.map(userViewIPage.getRecords(), new TypeToken<List<UserInfoBaseDto>>(){}.getType());
            listReturn.setRecords(list);
        }

        return listReturn;
    }

}
